#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void full_calloc_free(void)
{
    printf("申请3次内存，并释放\n");
    int *p_data[3];
    p_data[0] = malloc(sizeof(int));
    p_data[1] = malloc(sizeof(int) * 3);
    p_data[2] = malloc(sizeof(int) * 5);

    free(p_data[0]);
    free(p_data[1]);
    free(p_data[2]);
}

void manual_leak_mem(void)
{
    printf("申请3次内存，少释放一次,主动触发 内存泄漏\n");
    int *p_data[3];
    p_data[0] = malloc(sizeof(int));
    p_data[1] = malloc(sizeof(int) * 3);
    p_data[2] = malloc(sizeof(int) * 5);

    free(p_data[0]);
    free(p_data[1]);
    // free(p_data[2]);
}

int main(int argc, char *argv[])
{
    printf("argc=%d\n", argc);
    if (argc == 1)
        full_calloc_free();
    else
        manual_leak_mem();

    return 0;
}

/* 1. manual_leak_mem() 内存泄漏提示
2981   total heap usage: **4 allocs, 3 frees,** 1,060 bytes allocated -->==4次申请,3次释放,少一次free==
2981
2981 20 bytes in 1 blocks are definitely **lost in loss record** 1 of 1
2981    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
2981    by 0x1092A7: **manual_leak_mem (52_valgrind_内存泄漏提示.c:23)** -->==内存泄漏提示行==
2981    by 0x109318: main (52_valgrind_内存泄漏提示.c:37)
*/

/*2. full_calloc_free() 无内存泄漏提示
2921   total heap usage: **4 allocs, 4 frees**, 1,060 bytes allocated -->申请4次,释放4次
291
2921 All heap blocks were freed -- **no leaks are possible**  -->无内存泄漏可性
*/